﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using System.Net;
using System.Net.Http;
using System.Web.Http.Filters;

using Newtonsoft.Json.Converters;

using ProjectBase.Core;

namespace $safeprojectname$
{
    public class ErrorInputHandlerAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
            var service = context.ActionContext.ControllerContext.Controller as IEntityService;
            if (service.IsNull())
            {
                return;
            }

            var isErrorMessage = service.Messages.ContainsError();

            if (!isErrorMessage)
            {
                return;
            }

            var responseMessage = new ServiceMessage();
            foreach (var message in service.Messages)
            {
                responseMessage.Add(message.Severity, message.Text);
            }
            context.Response = context.Request.CreateResponse<ServiceMessage>(HttpStatusCode.BadRequest, responseMessage);
        }
    }
}